package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.Homework;
import com.wuseguang.springcloud.service.HomeworkService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 作业表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-01-16
 */
@RestController
@RequestMapping("/homework")
public class HomeworkController {

    public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private HomeworkService homeworkServiceImpl;

    /**
     * 查询班级作业
     * @param request
     * @param classId 班级id
     * @param indexNo 页码
     * @return
     */
    @RequestMapping("/findHomework")
    public Result findHomework(RequestParameterWrapper request,
                               @RequestParam(value = "classId", required = false) String[] classId,
                               @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                               @RequestParam (value = "rows" ,required = false,defaultValue = "20")Integer rows){
        logger.info("查询班级作业 HomeworkController -> findHomework");
        //教师id
        String teacherId = request.getParam("teacherId");

        //判断班级是否为空
        if(StringUtils.isEmpty(classId)){
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(),CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg()+"[班级未选择]");
        }

        return homeworkServiceImpl.findHomework(classId,indexNo,teacherId,rows);
    }

    /**
     * 添加作业
     * @param request
     * @param homework 作业信息
     * @return
     */
    @RequestMapping("/addHomework")
    public Result addHomework(RequestParameterWrapper request,@RequestBody Homework homework){
        logger.info("添加作业 HomeworkController -> addHomework");
        //教师id
        String teacherId = request.getParam("teacherId");
        //教师名称
        String teacherName = request.getParam("teacherName");
        //学期你
        String termId = request.getParam("termId");


        return homeworkServiceImpl.addHomework(homework,teacherId,teacherName,termId);
    }

    /**
     * 删除作业
     * @param request
     * @param id 作业id
     * @return
     */
    @RequestMapping("/deleteHomework")
    public Result deleteHomework(RequestParameterWrapper request,@RequestParam(value = "id", required = false) Integer id){

        logger.info("删除作业 HomeworkController -> deleteHomework");

        //教师id
        String teacherId = request.getParam("teacherId");
        return homeworkServiceImpl.deleteHomework(id,teacherId) ;
    }
}

